﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Timers;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Tasks.v1;
using Google.Apis.Tasks.v1.Data;
//using Google.Apis.Util;

namespace GoogleTasks
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            //ShowAllTasks();

            //Timer t = new Timer(20000);
            //t.Elapsed += new ElapsedEventHandler(t_Elapsed);
            //t.Enabled = true;
            NativeApplicationClient provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
            provider.ClientIdentifier = "288707121552.apps.googleusercontent.com";
            provider.ClientSecret = "vcTT5fC6NCmXZazBzcSk2K-z";
            var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, get
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {

        }

        private IAuthorizationState GetAuthentication(NativeApplicationClient arg)
        {
            // Get the auth URL:
            IAuthorizationState state = new AuthorizationState(new[] {  TasksService.Scopes.Tasks.GetStringValue() });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            Uri authUri = arg.RequestUserAuthorization(state);

            // Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString());
            Console.Write("  Authorization Code: ");
            string authCode = Console.ReadLine();
            Console.WriteLine();

            // Retrieve the access token by using the authorization code:
            return arg.ProcessUserAuthorization(authCode, state);
        }


        public void ShowAllTasks()
        {
            Outlook.MAPIFolder tasks = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
            
            foreach(Outlook.TaskItem ti in tasks.Items)
            {
                if (!ti.Complete)
                {
                    System.Windows.Forms.MessageBox.Show(ti.Subject + " " + ti.Body);
                }
            }

        }

        private void PopulateGoogleTasks()
        {

        }

        private void t_Elapsed(object source, ElapsedEventArgs e)
        {
            ShowAllTasks();
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
